package com.wjk.kylin.core.common.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.constant.CommonConstant;
import com.wjk.kylin.core.utils.SecurityUtils;
import lombok.Data;
import lombok.ToString;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * @author wangjkui
 * @description Entity支持类
 * @date 2020/5/22
 */
@Data
@ToString
public abstract class BaseEntity<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键id
     */
    @Null(message = "主键ID只能为空",groups = insert.class)
    @NotBlank(message = "主键ID不能为空",groups = update.class)
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    protected String id;

    /**
     * 创建时间
     */
    @TableField(value = "create_date", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date createDate;

    /**
     * 创建人
     */
    @TableField(value = "create_by", fill = FieldFill.INSERT)
    protected String createBy;

    /**
     * 更新时间
     */
    @TableField(value = "update_date", fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date updateDate;

    /**
     * 更新人
     */
    @TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
    protected String updateBy;

    /**
     * 备注
     */
    @Size(min = 0, max = 255, message = "备注不能超过255个字符",groups = {insert.class, update.class})
    @TableField("remark")
    protected String remark;

    /**
     * 是否删除（0 正常 1删除）
     */
//    @TableField("del_flag")
//    @TableLogic //todo mybatis plus 逻辑删除注解 . 自带查询会加上这个字段，删除也是设置为1
//    protected String delFlag;


    @TableField(exist = false)
    protected String beginCreateDate;//查询条件开始创建时间
    @TableField(exist = false)
    protected String endCreateDate;//查询条件结束创建时间


    public BaseEntity() {
        this.remark = REMARKS;
    }

    /**
     * 插入之前执行方法，需要手动调用
     */
    public void preInsert() {
        this.updateBy = SecurityUtils.getUsername();
        this.createBy = SecurityUtils.getUsername();
        this.updateDate = new Date();
        this.createDate = new Date();
    }

    /**
     * 更新之前执行方法，需要手动调用
     */
    public void preUpdate() {
        this.updateBy = SecurityUtils.getUsername();
        this.updateDate = new Date();
    }


    public static final String REMARKS = "0";
}
